//
//  SettingViewController.m
//  YunDongGym
//
//  Created by lqq on 2018/6/11.
//  Copyright © 2018年 lqq. All rights reserved.
//

typedef NS_ENUM(NSInteger, SettingVCCellType) {
    SettingVCCellType_Mobile,//手机
    SettingVCCellType_Account,//账号
    SettingVCCellType_ModifyPWD,//修改密码
    SettingVCCellType_FeedBack,//意见反馈
    SettingVCCellType_AboutYD,//关于云动
};

#import "SettingViewController.h"
#import "CommonTableViewCell.h"
#import "YDAlertViewController.h"
#import "ModifyPWDViewController.h"
#import "AboutYunDongController.h"
#import "YDSelectPopupView.h"
#import "I_Coach.h"


@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation SettingViewController

#pragma mark Class Method
+ (instancetype)controller
{
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    return settingVC;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    if (RI.account.loginWayType == LoginWayTypeByMobile) {
        _dataSource = @[@[@(SettingVCCellType_Mobile)],@[@(SettingVCCellType_AboutYD)]];
    } else if (RI.account.loginWayType == LoginWayTypeByAccount) {
        _dataSource = @[@[@(SettingVCCellType_Account),@(SettingVCCellType_ModifyPWD)],@[@(SettingVCCellType_AboutYD)]];
    } else if (RI.account.loginWayType == LoginWayTypeByWeChat) {
        _dataSource = @[@[@(SettingVCCellType_AboutYD)]];
    } else {
        _dataSource = @[@[@(SettingVCCellType_AboutYD)]];
    }
    
    [self configUI];

    
}
#pragma mark Config/UI
- (void)configUI{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = HEXColor(@"F4F6F9");
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    _tableView = tableView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 70)];
    UIButton *loginOutBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [loginOutBtn setTintColor:HEXColor(@"#333333")];
    [loginOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    loginOutBtn.titleLabel.font = RegularFont(14);
    loginOutBtn.backgroundColor = [UIColor whiteColor];
    [loginOutBtn addTarget:self action:@selector(loginOutBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:loginOutBtn];
    [loginOutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(footerView);
        make.height.equalTo(@(50));
    }];
    tableView.tableFooterView = footerView;
}

#pragma mark Action
- (void)loginOutBtnClicked
{
    [YDAlertViewController alertViewControllerWithMessage:@"确定退出登录吗？" cancleTitle:@"取消" confimTitle:@"确定" cancleBlock:nil confimBlock:^{
        [RI loginOut];
        
    }];
}


#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *list = _dataSource[section];
    return list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommonTableViewCell"];
    NSArray *list = _dataSource[indexPath.section];
    SettingVCCellType cellType = [list[indexPath.row] integerValue];
    switch (cellType) {
        case SettingVCCellType_Mobile:
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"手机号";
            cell.rightLabel.text = RI.account.loginMobile;
        }
            break;
        case SettingVCCellType_Account:
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"账号";
            cell.rightLabel.text = RI.account.loginAccount;
        }
            break;
        case SettingVCCellType_ModifyPWD:
        {
            cell.showStyle  = CommonTableViewShowStyleArrow;
            cell.leftLabel.text = @"修改密码";
        }
            break;
        case SettingVCCellType_FeedBack:
        {
            cell.showStyle = CommonTableViewShowStyleArrow;
            cell.leftLabel.text = @"意见反馈";
        }
            break;
        case SettingVCCellType_AboutYD:
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"关于云动管理";
            cell.rightLabel.text = [NSString stringWithFormat:@"v%@",[Toolkit appVersionNo]];
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSArray *list = _dataSource[indexPath.section];
    SettingVCCellType cellType = [list[indexPath.row] integerValue];
    switch (cellType) {
        case SettingVCCellType_Mobile:
        {
            
        }
            break;
        case SettingVCCellType_Account:
        {
            
        }
            break;
        case SettingVCCellType_ModifyPWD:
        {
            ModifyPWDViewController *modifyPWDVC = [ModifyPWDViewController controller];
            [self.navigationController pushViewController:modifyPWDVC animated:YES];
        }
            break;
        case SettingVCCellType_FeedBack:
        {
            YDWebViewController *feedBackWebVC = [YDWebViewController controllerWithUrl:@"https://wj.qq.com/s/2194002/853e"];
            [self.navigationController pushViewController:feedBackWebVC animated:YES];
        }
            break;
        case SettingVCCellType_AboutYD:
        {
            AboutYunDongController *aboutVC = [AboutYunDongController controller];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

@end
